package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserRealname> implements ApUserAuthService {
    @Autowired
    private ApUserAuthMapper apUserAuthMapper;

    /**
     * 审核失败
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = apUserAuthMapper.selectById(dto.getId());
        apUserRealname.setStatus((short) 2);
        apUserRealname.setReason(dto.getMsg());
        apUserAuthMapper.updateById(apUserRealname);
        return ResponseResult.okResult("已驳回");
    }

    /**
     * 审核通过
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = apUserAuthMapper.selectById(dto.getId());
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        apUserRealname.setStatus((short) 9);
        apUserAuthMapper.updateById(apUserRealname);
        return ResponseResult.okResult("已通过");
    }

    /**
     * 审核用户分页查询
     */
    @Override
    public ResponseResult PageList(AuthDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //创建分页条件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        //如果带状态,创建查询的条件
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null ) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());

        }

        //查询分页信息
        IPage<ApUserRealname> iPage = apUserAuthMapper.selectPage(page,query);
        //创建分页结果,分装
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        System.out.println(iPage.getRecords());
        //返回结果
        return pageResponseResult;

    }
}
